package com.grandbroad.eim.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.database.Observable;
import android.os.Bundle;
import android.os.Environment;
import android.support.multidex.MultiDexApplication;

import com.easy.west.westclient.MainActivity;
import com.easy.west.westclient.api.hotvod.api.HotVodImpl;
import com.easy.west.westclient.api.login.event.LoginEvent;
import com.easy.west.westclient.client.MoleClient;
import com.grandbroad.eim.R;
import com.grandbroad.eim.ui.activities.CollectActivity;
import com.grandbroad.eim.ui.activities.FollowListActivity;
import com.grandbroad.eim.ui.activities.HomeActivity;
import com.grandbroad.eim.ui.activities.LoginActivity;
import com.grandbroad.eim.ui.activities.PayActivity;
import com.grandbroad.eim.ui.activities.RegisterActivity;
import com.grandbroad.eim.ui.activities.SearchActivity;
import com.grandbroad.eim.ui.activities.VideoActivity;
import com.grandbroad.eim.utils.UiUtlis;
import com.jaeger.library.StatusBarUtil;
import com.orhanobut.hawk.Hawk;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


import org.greenrobot.eventbus.EventBus;
import org.xutils.common.util.LogUtil;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.event.ConversationRefreshEvent;
import dev.DevUtils;
import dev.utils.LogPrintUtils;
import dev.utils.app.logger.DevLogger;
import dev.utils.app.logger.LogConfig;
import dev.utils.app.logger.LogLevel;

import static com.tencent.wxop.stat.common.StatConstants.LOG_TAG;


public class MyApplication extends MultiDexApplication implements Application.ActivityLifecycleCallbacks {
    private final static String CACHE_DIR=Environment.getExternalStorageState()+"/moleshare/cache";
    private static Context context;
    private static MoleClient moleclient;
    public static final String WX_ID="wx238003fa37fd8ce3";
    public static final String WX_SCRECT="14bab96a7678bf9a5f8862afb45e20fc";
    private static List<Activity> activityList = new ArrayList<>();
    //微信api
    public static IWXAPI mWxApi;
    @Override
    public void onCreate() {
        super.onCreate();
        JMessageClient.init(this);
        registerToWX();
        initUtils();
        Hawk.init(getApplicationContext()).build();
        //清除微信登陆缓存

        x.Ext.init(this);
        registerActivityLifecycleCallbacks(this);
        context = this;
        moleclient = MoleClient.getInstance(this);
    }

    private void initUtils() {
        // 初始化工具类
        DevUtils.init(this.getApplicationContext());
        // == 初始化日志配置 ==
        // 设置默认Logger配置
        LogConfig logConfig = new LogConfig();
        logConfig.logLevel = LogLevel.DEBUG;
        logConfig.tag = LOG_TAG;
        DevLogger.init(logConfig);
        // 打开 lib 内部日志
        DevUtils.openLog();
        DevUtils.openDebug();
    }

    private void registerToWX() {
        //第二个参数是指你应用在微信开放平台上的AppID
        mWxApi = WXAPIFactory.createWXAPI(this,WX_ID, false);
        // 将该app注册到微信
        mWxApi.registerApp(WX_ID);
    }

    /**
     * 获取activity列表
     * @return
     */
    public static List<Activity> getActivityList() {
        return activityList;
    }

    /**
     * 添加activity
     * @param activity
     */
    private void addActicvity(Activity activity)
    {
        activityList.add(activity);
    }

    /**
     * 结束登陆activity
     * @param activity
     */
    public static void exitLoginActivity(Activity activity)
    {
        for (Activity activity1:activityList)
        {
            if (activity instanceof LoginActivity)
            {
                activity1.finish();
            }
        }
    }
    /**
     * 获取网络请求客户端
     * @return
     */
    public static MoleClient getMoleclient() {
        return moleclient;
    }

    /**
     * 注册eventbus
     * @param activity
     */
    public void regiestEvent(Activity activity)
    {
        if(!EventBus.getDefault().isRegistered(activity))
        {
            EventBus.getDefault().register(activity);
        }
    }

    /***
     * 注销eventbus
     * @param activity
     */
    public void unRegiestEvent(Activity activity)
    {
        EventBus.getDefault().unregister(activity);
    }
    /**
     * 获取上下文
     * @return
     */
    public static Context getContext() {
        return context;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        //设置通用状态栏颜色
        StatusBarUtil.setColor(activity,UiUtlis.getColor(R.color.Black_TranceLate),125);
        //添加
        addActicvity(activity);

    }

    @Override
    public void onActivityStarted(Activity activity) {
        //需要进行注册响应事件的在此处
        //首页
        //登陆界面
        if (activity instanceof LoginActivity)
        {
            regiestEvent(activity);
        }
        //注册界面
        if (activity instanceof RegisterActivity)
        {
            regiestEvent(activity);
        }
        //订单界面
        if (activity instanceof PayActivity)
        {
            regiestEvent(activity);
        }
        //搜索界面
        if (activity instanceof SearchActivity)
        {
            regiestEvent(activity);
        }
        //点播播放器界面
        if (activity instanceof VideoActivity)
        {
            regiestEvent(activity);
        }
        //关注列表界面
        if (activity instanceof FollowListActivity)
        {
            regiestEvent(activity);
        }
        //收藏列表界面
        if (activity instanceof CollectActivity)
        {
            regiestEvent(activity);
        }
    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {
        //视频播放界面
        if (activity instanceof VideoActivity)
        {
            //在后台不接受信息
            unRegiestEvent(activity);
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        //注销的在此处
        //可根据情况注销事件
        unRegiestEvent(activity);
    }

    /**
     * 初始化后的第一个事件（演示，这里接收不到）
     * @param eva
     */
    public void onEvent(ConversationRefreshEvent eva)
    {

    }




}
